/**
 * 
 */
package net.sf.tools.uiconfig.tags.config;

import java.util.List;

import javax.servlet.jsp.JspException;

import net.sf.tools.uiconfig.model.Configuration;
import net.sf.tools.uiconfig.tags.AbstractTag;

/**
 * @author sabuj.das
 * 
 */
public abstract class ConfigurationTag extends AbstractTag {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2366641623309677692L;

	private String pageName;
	private String sectionName;

	/**
	 * 
	 */
	public ConfigurationTag() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return the pageName
	 */
	public String getPageName() {
		return pageName;
	}

	/**
	 * @param pageName
	 *            the pageName to set
	 */
	public void setPageName(String pageName) {
		this.pageName = pageName;
	}

	/**
	 * @return the sectionName
	 */
	public String getSectionName() {
		return sectionName;
	}

	/**
	 * @param sectionName
	 *            the sectionName to set
	 */
	public void setSectionName(String sectionName) {
		this.sectionName = sectionName;
	}

	public abstract String getConfigParam();
	
	/**
	 * The configuration depends on the attributes. To load the complete page
	 * sections the sectionName attribute is not required. If the sectionName is
	 * specified for a particular page configuration, the session attribute will
	 * contain only the fields for that page and that section.
	 * 
	 * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
	 */
	@Override
	public int doStartTag() throws JspException {
		if(null != getHttpServletRequest().getSession().getAttribute(getConfigParam())){
			pageContext.getServletContext().log("The attribute already present in session: " + getConfigParam());
			return SKIP_BODY;
		}
		// set the list of fields in the config param
		List<Configuration> configurations = getConfigurationContext()
				.getConfigurationService().loadSectionConfigurations(
						getHttpServletRequest(), pageName);
		getHttpServletRequest().getSession().setAttribute(getConfigParam(),
				configurations);
		return SKIP_BODY;
	}

}
